from wxauto import WeChat
import uiautomation as auto

def ins_sendMsgTo(chat): # 发信息
    chat.SendMsg('收到')


def ins_getGroupMembers(wx,name): # 获取群聊成员名单
    wx.ChatWith(who=name)
    member_lst= wx.GetGroupMembers()
    reply=member_lst if member_lst else '该对象非群聊'
    return reply


def ins_addFriend(wx,keywords,remark,addmsg='你好，我是喵亲',tags=['朋友', '同事']): # 加好友
    # 发起好友申请
    wx.AddNewFriend(keywords, addmsg=addmsg, remark=remark, tags=tags)
    wx.SwitchToChat() # 由于搜索栏键盘未清空，如果连续触发加好友会出问题。所以先切换为微信主界面，下次再点就清空了。


def ins_acceptFriend(wx): # 接受好友申请
    new = wx.GetNewFriends()
    if len(new)<=0:
        wx.SwitchToChat()
        return None # 没有好友申请直接返回None
    new_friend1 = new[0]

    name=new_friend1.name  # 获取好友申请昵称
    msg=new_friend1.msg  # 获取好友申请信息

    # 接受好友请求，并且添加备注“备注张三”、添加标签wxauto
    # new_friend1.Accept(remark=new_friend1.name, tags=['wxauto'])
    new_friend1.Accept(remark=new_friend1.name) # tag 可以不加

    # 切换回聊天页面
    wx.SwitchToChat()
    return (name,msg)

def ins_voiceCall(wx,name):
    wx.ChatWith(name)
    wx_win=auto.WindowControl(ClassName='ChatWnd',Name=name,searchDepth=1)
    print(wx_win.Exists())
    if not wx_win.Exists():
        wx_win=auto.WindowControl(ClassName='WeChatMainWndForPC',Name='微信',searchDepth=1)
        print(wx_win.Exists())
    wx_win.SetFocus()
    voice_button=wx_win.ButtonControl(Name='语音聊天',LocalizedControlType='按钮',searchDepth=15)
    print(voice_button.Exists())
    if voice_button.Exists():
        voice_button.Click()
